<?php
/**
 * 角色管理页面
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: RolesController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Controller;
class RolesController extends CommonController {
    protected $model = NULL;
    protected $service = NULL;
    
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Roles");
        $this->service = D("Roles", "Service");
    }
    public function index() {
        $this->roleList = $this->service->getRoles();
        //var_dump($this->roleList);die;
        $this->roleCount = $this->service->getCount();
        $this->display();
    }
    
    /**
     * 添加角色信息
     */
    public function add() {
        $roleList = $this->service->getRoles();
        $this->assign('roleList', $roleList);
        $this->display();
    }
    
    /**
     * 创建角色信息
     * @return
     */
    public function create() {
        $roles = I("post.roles");
        if (!isArrayAndNotEmpty($roles)) {
            return $this->errorReturn('无效的操作！');
        }

        $result = $this->service->addRole($roles);
        if (!$result['status']) {
            return $this->errorReturn($result['data']['error']);
        }

        return $this->successReturn('添加角色成功！', U('Roles/index'));
    }
    
    /**
     * 编辑角色信息
     * @return
     */
    public function edit() {
        $roleid = I("get.id", 0, "intval");
        if ($roleid <= 0 || !$this->service->existRole($roleid)) {
            return $this->error('需要编辑的角色不存在！');
        }
        $role = $this->model->getById($roleid);
        $roleList = $this->service->getRoles();
        $this->assign('roleList', $roleList);
        
        $this->assign('role', $role);
        $this->display();
    }

    /**
     * 更新角色信息
     * @return
     */
    public function update() {
        $roles = I("post.roles");
        if (!isArrayAndNotEmpty($roles) || !$this->service->existRole($roles['id'])) {
            return $this->errorReturn('无效的操作！');
        }

        $result = $this->service->updateRole($roles);
        if (!$result['status']) {
            return $this->errorReturn($result['data']['error']);
        }

        return $this->successReturn('更新角色信息成功！', U('Roles/index'));
    }
    
    /**
     * 删除角色
     */
    public function delete() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            return $this->errorReturn('无效的操作！');
        }
        $result = $this->service->deleteRoles($id);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('删除角色成功！', U('index'));
    }
    
    /**
     * 权限分配
     * @return
     */
    public function assignAccess() {
        $id = I("get.id", 0, "intval");
        if ($id <= 0 || !$this->service->existRole($id)) {
            return $this->errorReturn('需要分配权限的角色不存在！');
        }

        $role = $this->model->getById($id);
        if (0 == $role['pid']) {
            return $this->error('您无权限进行该操作！');
        }

        $access = D('Access')->relation(true)
                             ->where("role_id={$role['id']}")
                             ->select();
        $rAccess = array();
        foreach ($access as $key => $item) {
            $rAccess[$key]['val'] = "{$item['node_id']}:"
                                    . "{$item['node']['level']}:"
                                    . "{$item['node']['pid']}";
        }

        $role['access'] = json_encode($rAccess);
        //var_dump(D('Nodes')->getLevelNodes());die;
        $this->assign('role', $role);
        $this->assign('nodes', D('Node', "Service")->getLevelNodes());
        $this->display('assign_access');
    }

    /**
     * 处理分配权限
     * @return
     */
    public function doAssignAccess() {
        $id = I("post.id");
        if ($id <= 0 || !$this->service->existRole($id)) {
            return $this->errorReturn('需要分配权限的角色不存在！');
        }

        $access = I("post.access");
        if (!isArrayAndNotEmpty($access)) {
            $access = array();
        }

        $result = $this->service->assignAccess($id, $access);
        if (!$result['status']) {
            return $this->errorReturn($result['data']['error']);
        }

        if (!empty($result['data'])) {
            return $this->successReturn($result['data']);
        }

        return $this->successReturn('分配权限成功！');
    }
    
    public function toggleStatus() {
        $id = I("post.id", 0, "intval");
        $status = I("post.status", NULL);
        if (!isset($id) || !$this->service->existRole($id)) {
            return $this->errorReturn('无效的操作！');
        }
        $RoleInfo = $this->service->getRoleInfo($id);
        if($RoleInfo['id'] == 1){
            return $this->errorReturn('超级管理员角色不能禁用！');
        }
        if ($status == 'yes') {
            $this->service->setStatus($id, 1);
        } else if($status === 'no'){
            $this->service->setStatus($id, 0);
        }else{
            return $this->errorReturn('无效的操作！');
        }
        $info = $status === 'no' ? '禁用成功！' : '启用成功！';
        $this->successReturn($info);
    }
}
